package com.example.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.example.service.HttpService;
import com.example.service.SqlHelper;
import com.example.tools.HttpRequestUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;

@Service
public class SqlHelperImpl implements SqlHelper {


    @Value("http://caiwu.whjdcs.com:20219/ChuanYunDataBase")
    private String sqlAddress;


    @Resource
    HttpService httpService;

    @Override
    public String updateData(String sql, String custId) {
        String jdyUrl = UriComponentsBuilder.fromUriString(sqlAddress + "/UpdateData")
                .queryParam("sql", sql)
                .queryParam("custid", custId)
                .build().encode().toString();
        return HttpRequestUtil.sendPost(jdyUrl, null, null);
    }

    @Override
    public String getValue(String sql, String custId) {
        String jdyUrl = sqlAddress + "/SelectData?sql=" + sql + "&custid=" + custId + "&limit=100";
        String result = httpService.sendPost(jdyUrl, null, null);
        while (result == "发送失败") {
            result = httpService.sendPost(jdyUrl, null, null);
        }
        return result;
    }

    @Override
    public Boolean Execute(String sql, String custId) {
        //请求路径
        String url = sqlAddress + "/SelectDataCol";
        //使用Restemplate来发送HTTP请求
        RestTemplate restTemplate = new RestTemplate();

        // LinkedMultiValueMap 有点像JSON，用于传递post数据，网络上其他教程都使用
        LinkedMultiValueMap body=new LinkedMultiValueMap();
        body.add("sql",sql);
        body.add("custid", custId);

        //设置请求header 为 APPLICATION_FORM_URLENCODED
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 请求体，包括请求数据 body 和 请求头 headers
        HttpEntity httpEntity = new HttpEntity(body, headers);
        Boolean reData = null;
        try {
            //使用 exchange 发送请求，以String的类型接收返回的数据
            ResponseEntity<String> strbody = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
            //解析返回的数据
            String reBody = strbody.getBody();
            JSONObject jsonObject = JSONObject.parseObject(reBody);
            String errMsg = "";
            if (jsonObject.getString("msg").isEmpty()) {
                reData = true;
            } else {
//                errMsg=jsonObject.getString("msg");
//                remindWechatMessege(custId,sql+": "+errMsg);
                reData = false;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
//        int index = sql.indexOf("(");
//        if(index!=-1){
//            String infoStatement=sql.substring(5,index);
//            if(sql.contains("PCCalc_Workman_Continue"))
//            {
//                int beginIndex = sql.lastIndexOf(",");
//                int endIndex = sql.lastIndexOf(")");
//                String calcCount = sql.substring(beginIndex+1,endIndex);
//                infoStatement = calcCount+". "+infoStatement;
//            }
//            calcPerformanceService.logInfo("【"+custId+"】"+infoStatement,reData);
//        }
        return reData;
    }


}
